package com.hsnn.medstgmini.pro.base.dao.Impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.base.dict.dao.impl.BaseHibernateDAO;
import com.hsnn.medstgmini.dao.HibernateSqlUtil;
import com.hsnn.medstgmini.pro.base.dao.WsjDelRelationDao;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 卫生局配送关系管理
 * @author wangbing
 * @date 2015年11月20日
 */
@Repository
public class WsjDelRelationDaoImpl extends BaseHibernateDAO implements WsjDelRelationDao{
	
	@Autowired
	private HibernateSqlUtil dao;
	
	/**
	 * @category 获取配送企业列表 给wsj申报
	 * @author wangbing
	 * @date   2015-11-20 10:49:39
	 * @param  @param page
	 * @param  @return Pagination
	 */
	@Override
	public Pagination getDelCompList(Pagination page) {
		String delCompCode = page.getConditions().get("delCompCode");
		String delCompName = page.getConditions().get("delCompName");
		String orgId = page.getConditions().get("orgId");
		StringBuffer sql = new StringBuffer()
		.append("	select comp_code delCompCode,comp_name delCompName from std_compinfo  where comp_type = 2")
		.append("	and comp_code not in (select del_comp_code from base_del_area_rel ")
		.append("	where area_code = (select area_id from std_healthbureau where hea_bur_id = :orgId))");
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("orgId", orgId);
		if(StringUtils.isNotBlank(delCompCode)){
			sql.append(" and comp_code = :delCompCode ");
			params.put("delCompCode", delCompCode.trim());
		}
		if(StringUtils.isNotBlank(delCompName)){
			sql.append(" and (comp_name like :delCompName or comp_spel_code like :delCompNameSpel)");
			params.put("delCompName", "%"+delCompName.trim()+"%");
			params.put("delCompNameSpel", "%"+delCompName.trim().toUpperCase()+"%");
		}
		return dao.getPageMapList(sql.toString(), params, page);
	}
	
	/**
	 * 
	 *@category wsj 申报一个配送企业 
	 *@author wangbing
	 *@date 2015-11-20 10:45:29
	 *@param page
	 *@return Pagination
	 */
	private int declareOneDelComp(Map<String,String> data){
		Session session = super.getSession();
		String updUser = data.get("updUser");
		String baseDelAreaRelId = UUID.randomUUID().toString();
		String delCompCode = data.get("delCompCode");
		String updDatetime = data.get("updDatetime");
		String orgId = data.get("orgId");
		StringBuffer sql = new StringBuffer()
		.append("	insert into base_del_area_rel select :baseDelAreaRelId base_del_area_rel_id,a.area_id area_code,a.area_name area_name,a.ar_shortname ar_shortname,")
		.append("	a.ar_fullname ar_fullname,b.comp_code del_comp_code,b.comp_name del_comp_name,b.comp_spel_code del_comp_name_spel,b.status del_comp_status,1 status,")
		.append("	null del_range,null sel_user,null sel_datetime,:updUser upd_user,:updDatetime upd_datetime ")
		.append("	from dic_area a,std_compinfo b where a.area_id =(select area_id from std_healthbureau where hea_bur_id = :orgId) and b.comp_code = :delCompCode ");
		Query query = session.createSQLQuery(sql.toString());
		query.setString("updUser", updUser);
		query.setString("updDatetime", updDatetime);
		query.setString("baseDelAreaRelId", baseDelAreaRelId);
		query.setString("delCompCode", delCompCode);
		query.setString("orgId", orgId);
		int count = query.executeUpdate();
		session.flush();
		return count;
	}
	
	/**
	 * 
	 *@category wsj 申报配送企业 
	 *@author wangbing
	 *@date 2015-11-20 10:45:29
	 *@param page
	 *@return Pagination
	 */
	@Override
	public int declareDelComp(List<Map<String,String>> list){
		int sum = 0;
		for(Map<String,String> data : list){
			int count = declareOneDelComp(data);
			sum += count;
		}
		return sum;
	}
	
	/**
	 * 
	 *@category wsj 获取遴选结果列表
	 *@author wangbing
	 *@date 2015-11-20 10:45:29
	 *@param 
	 *@return Pagination
	 */
	@Override
	public Pagination getSelectResultList(Pagination page){
		String delCompCode = page.getConditions().get("delCompCode");
		String delCompName = page.getConditions().get("delCompName");
		String delRange = page.getConditions().get("delRange");
		String status = page.getConditions().get("status");
		String orgId = page.getConditions().get("orgId");
		StringBuffer sql = new StringBuffer()
		.append("	select del_comp_code delCompCode,del_comp_name delCompName,status,del_range delRange,sel_datetime selDatetime")
		.append("	from base_del_area_rel where area_code = (select area_id from ")
		.append("	std_healthbureau where hea_bur_id = :orgId)");
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("orgId", orgId);
		if(StringUtils.isNotBlank(delCompCode)){
			sql.append(" and del_comp_code = :delCompCode ");
			params.put("delCompCode", delCompCode.trim());
		}
		if(StringUtils.isNotBlank(delCompName)){
			sql.append(" and del_comp_name like :delCompName or del_comp_name_spel like :delCompName ");
			params.put("delCompName", "%"+delCompName.trim()+"%");
		}
		if(StringUtils.isNotBlank(delRange)){
			sql.append(" and del_range = :delRange ");
			params.put("delRange", delRange);
		}
		if(StringUtils.isNotBlank(status)){
			sql.append(" and status = :status ");
			params.put("status", status);
		}
		return dao.getPageMapList(sql.toString(), params, page);
	}
}
